"""
<name>Survival Plot</name>
<author>Based on Plot which was Generated using Widget Maker written by Kyle R. Covington</author>
<description>Plots a survival object, usually from Survival Fit</description>
<icon>survival.png</icon>
<tags>Survival</tags>
<RFunctions>graphics:plot, survival:Surv</RFunctions>
"""
from OWRpy import * 
import OWGUI 
import redRGUI, signals
class survivalPlot(OWRpy): 
    settingsList = ['RFunctionParam_cex', 'RFunctionParam_main', 'RFunctionParam_xlab', 'RFunctionParam_ylab']
    def __init__(self, **kwargs):
        OWRpy.__init__(self, **kwargs)
        self.RFunctionParam_x = ''
        self.inputs.addInput("x", 'Survival Fit Data', signals.survival.RSurvFit, self.processx)
        
        box = redRGUI.base.widgetBox(self.controlArea, "Options Box")
        grid = redRGUI.base.gridBox(box)
        self.RFunctionParam_main = redRGUI.base.lineEdit(grid.cell(0,0), label = 'Main Title:', text = 'Survival Plot', toolTip = 'The title of the plot')
        self.RFunctionParam_xlab = redRGUI.base.lineEdit(grid.cell(1,0), label = 'X Axis Label:', text = 'Time', toolTip = 'The text representing the X axis.')
        self.RFunctionParam_ylab = redRGUI.base.lineEdit(grid.cell(2,0), label = 'Y Axis Label:', text = 'Percent Surviving', toolTip = 'The text representing the Y axis.')
        self.RFunctionParam_xscale = redRGUI.base.lineEdit(grid.cell(0,1), label = 'X Scale Adjust:', toolTip = 'Number to adjust the scale by to convert from different times ex months to years.')
        self.RFunctionParam_colors = redRGUI.base.lineEdit(grid.cell(1,1), label = 'Colors:', toolTip = 'Colors either as names (must be in quotes) or numbers')
        self.RFunctionParam_cex = redRGUI.base.lineEdit(grid.cell(2,1), label = 'Text Magnification Percent:', text = '120', toolTip = 'Magnification of the axes.')
        self.plotArea = redRGUI.base.graphicsView(self.controlArea)
        redRGUI.base.commitButton(self.bottomAreaRight, "Commit", callback = self.commitFunction)
    
    def processx(self, data):
        if data:
            self.RFunctionParam_x=data.getData()
            self.commitFunction()
        else:
            self.RFunctionParam_x = ''
    def commitFunction(self):
        #if self.RFunctionParam_y == '': return
        if self.RFunctionParam_x == '': return
        injection = []
        if self.RFunctionParam_main != '':
            injection.append('main = "'+str(self.RFunctionParam_main.text())+'"')
        if self.RFunctionParam_xlab != '':
            injection.append('xlab = "'+str(self.RFunctionParam_xlab.text())+'"')
        if self.RFunctionParam_ylab != '':
            injection.append('ylab = "'+str(self.RFunctionParam_ylab.text())+'"')
        if self.RFunctionParam_xscale != '':
            injection.append('xscale = '+str(self.RFunctionParam_xscale.text()))
        if self.RFunctionParam_cex != '100':
            mag = float(str(self.RFunctionParam_cex.text()))/100
            injection.append('cex.lab = '+str(mag))
            injection.append('cex.axis = '+str(mag))
        if str(self.RFunctionParam_colors.text()) != '':
            injection.append('col = c('+str(self.RFunctionParam_colors.text())+')')
        inj = ','.join(injection)
        
        self.plotArea.plot(query = str(self.RFunctionParam_x)+','+inj+', yscale = 100, lty=c(1,2,3)', dwidth = 5, dheight = 5)
